package com.atguigu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.service.TravelGroupService;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/TravelGroup")
public class TravelGroupController {
    @Reference
    private TravelGroupService travelGroupService;

    @RequestMapping("/add")
    public Result add(@RequestBody TravelGroup travelGroup, Integer[] travelItemIds) {//接收两部分数据
        try {
            travelGroupService.add(travelGroup, travelItemIds);
            return new Result(true, MessageConstant.ADD_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_TRAVELGROUP_FAIL);
        }
    }


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = travelGroupService.findPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    @RequestMapping("/get")
    public Result get(Integer id) {
        try {
            TravelGroup travelGroup = travelGroupService.get(id);
            return new Result(true, MessageConstant.QUERY_TRAVELGROUP_SUCCESS, travelGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    @RequestMapping("/getTravelItemIdByTravelGroupId")
    public Result getTravelItemIdByTravelGroupId(Integer travelGroupId) {
        try {
            List<Integer> travelItemIds = travelGroupService.getTravelItemIdByTravelGroupId(travelGroupId);
            return new Result(true, MessageConstant.QUERY_TRAVELITEMID_SUCCESS,travelItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_TRAVELITEMID_FAIL);
        }
    }

        @RequestMapping("/edit")
        public Result edit(@RequestBody TravelGroup travelGroup, Integer[] travelItemIds){
            try {
                travelGroupService.edit(travelGroup,travelItemIds);
                return new Result(true,MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,MessageConstant.EDIT_TRAVELGROUP_FAIL);
            }

        }
        @RequestMapping("/delete")
        public Result delete(Integer id){//省略了@RequestParam("id")
            try {
                travelGroupService.delete(id);
                return new Result(true,MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
            } catch (RuntimeException e) {
                e.printStackTrace();
                return new Result(false,e.getMessage());
            }catch (Exception e) {
                e.printStackTrace();
                return new Result(false,MessageConstant.DELETE_TRAVELGROUP_FAIL);
            }
        }

        @RequestMapping("/findAll")
    public Result findAll(){
            try {
                List<TravelGroup> list=travelGroupService.findAll();
                return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,list);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
            }

        }

}

